package com.interview.controller;

import com.interview.feign.FeignInterviewService;
import com.interview.pojo.Article;
import com.interview.utils.JsonTurnUtils;
import com.interview.utils.Page;
import com.interview.utils.Result;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feign_article")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class FeignArticleController {

    @Autowired
    private FeignInterviewService feignArticleService;

//HttpServletRequest request
    //http://localhost:8083/feign_article
    @PostMapping
    @HystrixCommand
    public String add(@RequestBody Article article) {
        feignArticleService.add(article);

        return "添加成功";
    }


    //http://localhost:8083/feign_article
    @PostMapping("/del")
    @HystrixCommand
    public String del(@RequestBody Article article){
        System.out.println(article);
        feignArticleService.del(article);
        return "删除成功";
    }
    @PostMapping("up")
    @HystrixCommand
    public String up(@RequestBody Article article){
        feignArticleService.up(article);
        return "修改成功";
    }



    //http://localhost:8083/feign_article/queryRecommend
    @GetMapping("/queryRecommend")
    @HystrixCommand
    public String  queryRecommend(){
        Result<List<Article>> listResult = feignArticleService.queryRecommend();
        String s = JsonTurnUtils.objTurnJson(listResult);
        return s;
    }
    //http://localhost:8083/feign_article/queryRandom
    @GetMapping("/queryRandom")
    @HystrixCommand
    public  String queryRandom (){
        Result<List<Article>> listResult = feignArticleService.queryRandom();
        String s = JsonTurnUtils.objTurnJson(listResult);
        return   s;
    }


    //http://81.70.77.136:8083/feign_article/queryAll/1/10
    //http://localhost:8083/feign_article/queryAll/1/10
    @GetMapping("/queryAll/{current_Page}/{page_Size}")
    @HystrixCommand
    public String queryAll(@PathVariable("current_Page") int current_Page , @PathVariable("page_Size") int page_Size) throws Exception {

        List<Article> list = feignArticleService.queryAll_Article();
        Page<Article> page=new Page(current_Page,page_Size, list);
        String s = JsonTurnUtils.objTurnJson(page);
        System.out.println(s);

        return   s;
    }

    //http://localhost:8083/feign_article/count
    @GetMapping("/count")
    @HystrixCommand
    public String  queryAll_Count(){
        int i = feignArticleService.queryAll_Count();
        String s = JsonTurnUtils.objTurnJson(i);
        return s;
    }

    //http://localhost:8083/feign_article/count_time
    @GetMapping("/count_time")
    public String count_time(@RequestParam("time1") String time1,@RequestParam("time2")String time2){
        int i = feignArticleService.count_time(time1, time2);
        String s = JsonTurnUtils.objTurnJson(i);
        return s;
    }


    //http://localhost:8083/feign_article/query/1/20?catalogue_id=66-2021jU1zZ7yS9jO0zO6p445E-923
    @GetMapping("/query/{current_Page}/{page_Size}")
    @HystrixCommand
    public String query(@RequestParam("catalogue_id") String catalogue_id,@PathVariable("current_Page") int current_Page ,@PathVariable("page_Size") int page_Size) throws Exception {
        String s = null;
        try {
            List<Article> query1 = feignArticleService.query(catalogue_id);
            Page<Article> page=new Page(current_Page,page_Size,query1);
            s = JsonTurnUtils.objTurnJson(page);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return s;
    }
    //http://localhost:8083/feign_article/article/query_Name/1/10?head=java
    @GetMapping("/article/query_Name/{current_Page}/{page_Size}")
    public String query_Name(@RequestParam("head") String head,@PathVariable("current_Page") int current_Page ,@PathVariable("page_Size") int page_Size) throws Exception {
        List<Article> list = feignArticleService.query_Name(head);
        Page<Article> page=new Page(current_Page,page_Size,list);
       String s = JsonTurnUtils.objTurnJson(page);
       return  s;
    }



    //http://localhost:8083/feign_article/articleId?articleId=928-2021qZ1kH7kD9nW57xW46d813B-964
    @GetMapping("/articleId")
    @HystrixCommand
    public String queryID(@RequestParam("articleId") String articleId){
        Result<Article> articleResult = feignArticleService.queryID(articleId);
         String s = JsonTurnUtils.objTurnJson(articleResult);
        return  s ;
    }


    //降级方法
    public String payment_Global_FallbackMethod() {
        String str = "<div style=\"width: 100%;text-align: center\">\n" +
                "    <h1 >对不起，网络太拥挤了！ 刷新重试-----Feign</h1>\n" +
                "</div>";
        return str;
    }



    //http://localhost:8083/feign_article/hystrix
    //测试 熔断是否触发
    @GetMapping("/hystrix")
    @ResponseBody
    @HystrixCommand
    public String testHystrix() throws InterruptedException {
        //这个触发了 feign-service 内的 超时处理
       Thread.sleep(5000);

       return  "超时";
    }

}
